let logger = require("./Logger");

/**
 * 根据rect对象随机一个范围内的坐标
 * @param bounds
 * @return {{x: (*|number), y: (*|number)}}
 */
function randPos(bounds) {
    const randX = random(Math.ceil(bounds.left), Math.floor(bounds.right));
    const randY = random(Math.ceil(bounds.bottom), Math.floor(bounds.top));
    return {x: randX, y: randY};
}

/**
 * 等待具体时间
 * @param ms
 */
function wait(ms) {
    sleep(ms);
}

/**
 * 随机等待
 * @param min
 * @param max
 */
function randWait(min, max) {
    const randWaitMs = random(min, max);
    logger.info(`随机等待${randWaitMs}毫秒...`);
    sleep(randWaitMs);
}

/**
 * 随机点击空间内部一个位置
 * @param widget
 */
function randClick(widget) {
    const bounds = widget.bounds();
    const clickPos = randPos(bounds);
    click(clickPos.x, clickPos.y);
}

/**
 * 随机press
 * @param widget
 * @param duration
 */
function randPress(widget, duration) {
    const bounds = widget.bounds();
    const clickPos = randPos(bounds);
    press(clickPos.x, clickPos.y, duration);
}

module.exports = {
    randPos,
    randClick,
    randPress,
    wait,
    randWait,
}